package com.fone.player.storage;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import android.app.NotificationManager;
import android.content.Context;
import android.os.Environment;

import com.fone.player.constant.FoneConstant;
import com.fone.player.entity.ContentSubscribe;
import com.fone.player.entity.CustomChannel;
import com.fone.player.entity.Download;
import com.fone.player.entity.NotificationBean;
import com.fone.player.entity.OfflineCache;
import com.fone.player.entity.PlayRecord;
import com.fone.player.storage.download.CacheDownloadManager;
import com.fone.player.storage.download.DownloadUtil;
import com.fone.player.storage.download.NotificationHandle;
import com.fone.player.util.L;
import com.fone.player.util.SystemUtil;

/**
 * 
 * 文件模块.<br>
 * 与文件相关操作都在此模块.<br>
 * 通过StorageModule.getInstance()得到该类实例,然后操作该类方法.<br>
 * Example:<br>
 * 
 * <pre>
 * StorageModule.getInstance().getVideoList(StorageModule.LIST_TYPE_NORMAL_VIDEO);
 * </pre>
 * 
 * @author wangyang
 * @since 2014年4月29日
 */
public class StorageModule {
	private static final String TAG = StorageModule.class.getSimpleName();
	/**
	 * 扫描媒体库文件开始
	 */
	public static final int MSG_ACTION_MEDIA_SCANNER_STARTED = 1;
	/**
	 * 扫描媒体库文件结束
	 */
	public static final int MSG_ACTION_MEDIA_SCANNER_FINISHED = 2;
	/**
	 * 扫描全盘文件开始
	 */
	public static final int MSG_ACTION_SCANNER_STARTED = 3;
	/**
	 * 扫描全盘文件进行中
	 */
	public static final int MSG_ACTION_SCANNER_PROCESSING = 4;
	/**
	 * 扫描全盘文件结束
	 */
	public static final int MSG_ACTION_SCANNER_FINISHED = 5;
	/**
	 * 扫描全盘文件停止
	 */
	public static final int MSG_ACTION_SCANNER_STOPPED = 6;
	/**
	 * 普通文件夹全部列表返回成功
	 */
	public static final int MSG_NORMAL_FOLDER_LIST_SUCCESS = 101;
	/**
	 * 加密文件全部列表返回成功
	 */
	public static final int MSG_ENCRYPT_FILE_LIST_SUCCESS = 102;
	/**
	 * 普通文件全部列表返回成功
	 */
	public static final int MSG_NORMAL_FILE_LIST_SUCCESS = 103;
	/**
	 * 文件夹中文件全部列表返回成功
	 */
	public static final int MSG_FOLDER_FILE_LIST_SUCCESS = 104;
	/**
	 * 普通文件全部列表
	 */
	public static final int LIST_TYPE_FILE_NORMAL_VIDEO = 201;
	/**
	 * 加密文件全部列表
	 */
	public static final int LIST_TYPE_FILE_ENCRYPT_VIDEO = 202;
	/**
	 * 文件夹全部列表
	 */
	public static final int LIST_TYPE_FOLDER_NORMAL_VIDEO = 203;
	/**
	 * 缓存界面中文件夹列表返回成功
	 */
	public static final int MSG_FOLDER_CACHE_UPDATE_PROGRESS_SUCCESS = 301;
	/**
	 * 缓存界面中文件列表返回成功
	 */
	public static final int MSG_FILE_CACHE_UPDATE_PROGRESS_SUCCESS = 302;
	/**
	 * 收藏界面中文件夹列表返回成功
	 */
	public static final int MSG_FOLDER_FAVOURITE_LIST_SUCCESS = 303;
	/**
	 * 收藏界面中文件列表返回成功
	 */
	public static final int MSG_FILE_FAVOURITE_LIST_SUCCESS = 304;
	/**
	 * 缓存界面中文件夹列表返回成功
	 */
	public static final int MSG_FOLDER_CACHE_LIST_SUCCESS = 305;
	/**
	 * 缓存界面中文件列表返回成功
	 */
	public static final int MSG_FILE_CACHE_LIST_SUCCESS = 306;
	/**
	 * 无网络
	 */
	public static final int MSG_NO_NETWORK_TYPE = 401;
	/**
	 * wifi网络
	 */
	public static final int MSG_WIFI_NETWORK_TYPE = 402;
	/**
	 * 非wifi网络
	 */
	public static final int MSG_UNWIFI_NETWORK_TYPE = 403;
	/**
	 * 3G网络
	 */
	public static final int MSG_3G_NETWORK_TYPE = 404;
	/**
	 * 2G网络
	 */
	public static final int MSG_2G_NETWORK_TYPE = 405;
	/**
	 * 移动网络
	 */
	public static final int MSG_YIDONG_NETWORK_TYPE = 406;
	/**
	 * 联通网络
	 */
	public static final int MSG_LIANTONG_NETWORK_TYPE = 407;
	/**
	 * 电信网络
	 */
	public static final int MSG_DIANXIN_NETWORK_TYPE = 408;
	/**
	 * 4G网络
	 */
	public static final int MSG_4G_NETWORK_TYPE = 409;
	/**
	 * 未知网络
	 */
	public static final int MSG_UNKOWN_NETWORK_TYPE = 410;

	/**
	 * 电信wap网络
	 */
	public static final String WAP_CT = "ctwap";
	/**
	 * 移动wap网络
	 */
	public static final String WAP_CM = "cmwap";
	/**
	 * 联通wap网络
	 */
	public static final String WAP_3G = "3gwap";
	/**
	 * 联通wap网络
	 */
	public static final String WAP_UNI = "uniwap";

	/**
	 * 添加任务成功
	 */
	public static final int MSG_ADD_CACHE_SUCCESS = 501;
	/**
	 * 添加任务失败
	 */
	public static final int MSG_ADD_CACHE_FAIL = 502;
	/**
	 * 下载任务成功
	 */
	public static final int MSG_DOWNLOAD_CACHE_SUCCESS = 503;
	/**
	 * 删除下载任务成功
	 */
	public static final int MSG_DELETE_CACHE_SUCCESS = 504;
	/**
	 * 边下边播,已下载15%以上
	 */
	public static final int MSG_DOWNLOAD_PREPARE_ONLINE_CACHE_SUCCESS = 507;
	/**
	 * 空间不足
	 */
	public static final int MSG_MIN_SPACE = 601;
	/**
	 * 达到下载上限
	 */
	public static final int MSG_MAX_DOWNLOAD = 602;
	/**
	 * 该视频暂时无法缓存
	 */
	public static final int MSG_CACHE_ERROR = 603;
	/**
	 * 文件下载中消息
	 */
	public static final int MSG_DOWNLOAD_PROGRESS = 701;
	/**
	 * 文件下载完成消息
	 */
	public static final int MSG_DOWNLOAD_SUCCESS = 702;
	/**
	 * 文件下载失败消息
	 */
	public static final int MSG_DOWNLOAD_ERROR = 703;
	/**
	 * 缓存开始全部成功
	 */
	public static final int MSG_CACHE_START_ALL_SUCCESS = 704;
	/**
	 * 缓存暂停全部成功
	 */
	public static final int MSG_CACHE_PAUSE_ALL_SUCCESS = 705;
	/**
	 * 本地存储空间进度变化
	 */
	public static final int MSG_STORAGE_SPACE_PROGRESS = 706;
	private static final StorageModule mInstance = new StorageModule();
	private static Context sContext;

	/**
	 * 获取文件模块的对象.<br>
	 * 单例静态方法.
	 * 
	 * @return 文件模块对象
	 */
	public static StorageModule getInstance() {
		return mInstance;
	}

	private StorageModule() {

	}

	/**
	 * 初始化{@link com.fone.player.storage.StorageModule StorageModule}
	 * 
	 * @param context
	 *            当前上下文
	 */
	public static void init(Context context) {

		if (context == null) {
			throw new NullPointerException(
					"StorageModule.init(Context context) context is null,please call init() method");
		}

		FoneDatabase.init(context);
		FileLocalModule.init(context);
		OfflineCacheModule.init(context);
		SystemUtil.init(context);
		// 初始化通知模块
		NotificationHandle.init(context);
		sContext = context;

		List<Download> downloadList = FileDownloadDataBaseAdapter.getInstance()
				.getDownloadList(Download.DOWNLOAD_STATE_ALL);
		if (downloadList != null) {
			// 清理之前的通知栏
			NotificationManager notificationManager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
			for (Download download : downloadList) {
				notificationManager
						.cancel(download.getDownloadNotificationId());
			}
		}

	}

	/**
	 * 获取本地视频<strong>文件</strong>列表.<br>
	 * 同步方法.
	 * 
	 * @param eventBus
	 *            消息传递者
	 * @param listType
	 *            列表类型<br>
	 *            {@link com.fone.player.storage.StorageModule.LIST_TYPE_NORMAL_VIDEO
	 *            StorageModule.LIST_TYPE_NORMAL_VIDEO} 媒体库文件全部列表<br>
	 *            {@link com.fone.player.storage.StorageModule.LIST_TYPE_NORMAL_VIDEO
	 *            StorageModule.LIST_TYPE_ENCRYPT_VIDEO} 加密文件全部列表<br>
	 * @return ArrayList&lt;{@link com.fone.player.entity.MediaFile MediaFile}
	 *         &gt;<br>
	 *         状态码(msg.what):<br>
	 *         {@link com.fone.player.storage.StorageModule.MSG_ENCRYPT_FILE_LIST_SUCCESS
	 *         StorageModule.MSG_ENCRYPT_FILE_LIST_SUCCESS} 加密文件全部列表返回成功<br>
	 *         {@link com.fone.player.storage.StorageModule.MSG_NORMAL_FILE_LIST_SUCCESS
	 *         StorageModule.MSG_NORMAL_FILE_LIST_SUCCESS} 普通文件全部列表返回成功<br>
	 *         集合键(Bundle):<br>
	 *         {@link com.fone.player.entity.MediaFile.MEDIA_FILE_LIST
	 *         MediaFile.MEDIA_FILE_LIST} 文件列表<br>
	 *         Example:<br>
	 * 
	 *         <pre>
	 * public void onEventMainThread(Message msg) {
	 * 	switch (msg.what) {
	 * 	case StorageModule.MSG_ENCRYPT_FILE_LIST_SUCCESS:
	 * 		Bundle bundle = msg.getData();
	 * 		if (bundle != null) {
	 * 			ArrayList&lt;MediaFile&gt; mediaFileList = bundle
	 * 					.getParcelableArrayList(MediaFile.MEDIA_FILE_LIST);
	 * 		}
	 * 		break;
	 * 	case StorageModule.MSG_NORMAL_FILE_LIST_SUCCESS:
	 * 		Bundle bundle = msg.getData();
	 * 		if (bundle != null) {
	 * 			ArrayList&lt;MediaFile&gt; mediaFileList = bundle
	 * 					.getParcelableArrayList(MediaFile.MEDIA_FILE_LIST);
	 * 		}
	 * 		break;
	 * 		break;
	 * 	}
	 * }
	 * </pre>
	 * @throws IllegalArgumentException
	 *             listType不在取值范围
	 */
	public void getVideoFileList(int listType) {

		FileLocalModule.getInstance().getVideoFileList(listType);

	}

	/**
	 * 获取本地视频<strong>文件夹</strong>列表.<br>
	 * <font color="red">注意</font>:获取该集合后,调用StorageModule.
	 * getVideoFileListByFolderPath( String folderPath),获取相应文件夹下的文件集合<br>
	 * 同步方法.
	 * 
	 * @param eventBus
	 *            消息传递者
	 * @return ArrayList&lt;{@link com.fone.player.entity.MediaFolder
	 *         MediaFolder} &gt;<br>
	 *         状态码(msg.what):<br>
	 *         {@link com.fone.player.storage.StorageModule.MSG_NORMAL_FOLDER_LIST_SUCCESS
	 *         StorageModule.MSG_NORMAL_FOLDER_LIST_SUCCESS} 普通文件夹全部列表返回成功<br>
	 *         集合键(Bundle):<br>
	 *         {@link com.fone.player.entity.MediaFolder.MEDIA_FOLDER_LIST
	 *         MediaFolder.MEDIA_FOLDER_LIST} 文件夹列表<br>
	 *         Example:<br>
	 * 
	 *         <pre>
	 * public void onEventMainThread(Message msg) {
	 * 	switch (msg.what) {
	 * 	case StorageModule.MSG_NORMAL_FOLDER_LIST_SUCCESS:
	 * 		Bundle bundle = msg.getData();
	 * 		if (bundle != null) {
	 * 			ArrayList&lt;MediaFolder&gt; mediaFolderList = bundle
	 * 					.getParcelableArrayList(MediaFolder.MEDIA_FOLDER_LIST);
	 * 		}
	 * 		break;
	 * 	}
	 * }
	 * </pre>
	 */
	public void getVideoFolderList() {
		FileLocalModule.getInstance().getVideoFolderList();
	}

	/**
	 * 根据文件夹路径获取文件列表.<br>
	 * 同步方法.
	 * 
	 * @param eventBus
	 *            消息传递者
	 * @param folderPath
	 *            待搜索文件夹路径
	 * @return ArrayList&lt;{@link com.fone.player.entity.MediaFile MediaFile}
	 *         &gt;<br>
	 *         状态码(msg.what):<br>
	 *         {@link com.fone.player.storage.StorageModule.MSG_FOLDER_FILE_LIST_SUCCESS
	 *         StorageModule.MSG_FOLDER_FILE_LIST_SUCCESS} 文件夹中文件全部列表返回成功<br>
	 *         集合键(Bundle):<br>
	 *         {@link com.fone.player.entity.MediaFile.MEDIA_FILE_LIST
	 *         MediaFile.MEDIA_FILE_LIST} 文件列表<br>
	 *         Example:<br>
	 * 
	 *         <pre>
	 * public void onEventMainThread(Message msg) {
	 * 	switch (msg.what) {
	 * 	case StorageModule.MSG_FOLDER_FILE_LIST_SUCCESS:
	 * 		Bundle bundle = msg.getData();
	 * 		if (bundle != null) {
	 * 			ArrayList&lt;MediaFile&gt; mediaFileList = bundle
	 * 					.getParcelableArrayList(MediaFile.MEDIA_FILE_LIST);
	 * 		}
	 * 		break;
	 * 	}
	 * }
	 * </pre>
	 * @throws NullPointerException
	 * <br>
	 *             folderPath为null
	 * @throws IllegalArgumentException
	 * <br>
	 *             folderPath为空字符串
	 * 
	 */
	public void getVideoFileListByFolderPath(String folderPath) {

		if (folderPath == null) {
			throw new NullPointerException(
					"StorageModule.getVideoFileListByFolderPath(List<MediaFolder> mediaFolderList, String folderPath) folderPath is null,Please re assignment!");
		}
		if ("".equals(folderPath)) {
			throw new IllegalArgumentException(
					"StorageModule.getVideoFileListByFolderPath(List<MediaFolder> mediaFolderList, String folderPath) folderPath is empty,Please re assignment!");
		}

		FileLocalModule.getInstance().getVideoFileListByFolderPath(folderPath);
	}

	/**
	 * 主动扫描外部存储器.<br>
	 * (请求系统广播,让系统自带扫描器扫描(机身<font
	 * color="red">或</font>SD卡),扫描路径:Environment.getExternalStorageDirectory ()
	 * .getAbsolutePath()<br>
	 * 异步方法
	 * 
	 * @param eventBus
	 *            消息传递者 <br>
	 * @return 状态码 (msg.what):<br>
	 *         {@link com.fone.player.storage.StorageModule.MSG_ACTION_MEDIA_SCANNER_STARTED
	 *         StorageModule.MSG_ACTION_MEDIA_SCANNER_STARTED} 扫描文件开始<br>
	 *         {@link com.fone.player.entity.MediaFolder.MSG_ACTION_MEDIA_SCANNER_FINISHED
	 *         StorageModule.MSG_ACTION_MEDIA_SCANNER_FINISHED} 扫描文件结束<br>
	 *         Example:<br>
	 * 
	 *         <pre>
	 * public void onEventMainThread(Message msg) {
	 * 	switch (msg.what) {
	 * 	case StorageModule.MSG_ACTION_MEDIA_SCANNER_STARTED:
	 * 		break;
	 * 	case StorageModule.MSG_ACTION_MEDIA_SCANNER_FINISHED:
	 * 		break;
	 * 	}
	 * }
	 * </pre>
	 */
	public void scanningExternalStorage() {
		FileLocalModule.getInstance().scanningExternalStorage();
	}

	/**
	 * 主动扫描外部存储器.<br>
	 * (手动扫描系统可能存在的所有外部存储器(机身+SD卡))<br>
	 * 异步方法
	 * 
	 * @param eventBus
	 *            消息传递者 <br>
	 * @return 状态码 (msg.what):<br>
	 *         {@link com.fone.player.storage.StorageModule.MSG_ACTION_SCANNER_STARTED
	 *         StorageModule.MSG_ACTION_SCANNER_STARTED} 扫描全盘文件开始<br>
	 *         {@link com.fone.player.entity.MediaFolder.MSG_ACTION_SCANNER_PROCESSING
	 *         StorageModule.MSG_ACTION_SCANNER_PROCESSING} 扫描全盘文件进行中<br>
	 *         {@link com.fone.player.entity.MediaFolder.MSG_ACTION_SCANNER_FINISHED
	 *         StorageModule.MSG_ACTION_SCANNER_FINISHED} 扫描全盘文件结束<br>
	 *         {@link com.fone.player.entity.MediaFolder.MSG_ACTION_SCANNER_STOPPED
	 *         StorageModule.MSG_ACTION_SCANNER_STOPPED} 扫描全盘文件停止<br>
	 *         Example:<br>
	 * 
	 *         <pre>
	 * public void onEventMainThread(Message msg) {
	 * 	switch (msg.what) {
	 * 	case StorageModule.MSG_ACTION_SCANNER_STARTED:
	 * 		break;
	 * 	case StorageModule.MSG_ACTION_SCANNER_PROCESSING:
	 * 		break;
	 * 	case StorageModule.MSG_ACTION_SCANNER_FINISHED:
	 * 		break;
	 * 	case StorageModule.MSG_ACTION_SCANNER_STOPPED:
	 * 		break;
	 * 	}
	 * }
	 * </pre>
	 */
	public void scanningAllStorage() {
		FileLocalModule.getInstance().scanningAllStorage();
	}

	/**
	 * 主动扫描媒体库.<br>
	 * (手动扫描系统媒体库)<br>
	 * 异步方法
	 * 
	 * @param eventBus
	 *            消息传递者 <br>
	 * @return 状态码 (msg.what):<br>
	 *         {@link com.fone.player.storage.StorageModule.MSG_ACTION_SCANNER_STARTED
	 *         StorageModule.MSG_ACTION_SCANNER_STARTED} 扫描全盘文件开始<br>
	 *         {@link com.fone.player.entity.MediaFolder.MSG_ACTION_SCANNER_PROCESSING
	 *         StorageModule.MSG_ACTION_SCANNER_PROCESSING} 扫描全盘文件进行中<br>
	 *         {@link com.fone.player.entity.MediaFolder.MSG_ACTION_SCANNER_FINISHED
	 *         StorageModule.MSG_ACTION_SCANNER_FINISHED} 扫描全盘文件结束<br>
	 *         {@link com.fone.player.entity.MediaFolder.MSG_ACTION_SCANNER_STOPPED
	 *         StorageModule.MSG_ACTION_SCANNER_STOPPED} 扫描全盘文件停止<br>
	 *         Example:<br>
	 * 
	 *         <pre>
	 * public void onEventMainThread(Message msg) {
	 * 	switch (msg.what) {
	 * 	case StorageModule.MSG_ACTION_SCANNER_STARTED:
	 * 		break;
	 * 	case StorageModule.MSG_ACTION_SCANNER_PROCESSING:
	 * 		break;
	 * 	case StorageModule.MSG_ACTION_SCANNER_FINISHED:
	 * 		break;
	 * 	case StorageModule.MSG_ACTION_SCANNER_STOPPED:
	 * 		break;
	 * 	}
	 * }
	 * </pre>
	 */
	public void scanningMediaStore() {
		FileLocalModule.getInstance().scanningMediaStore();
	}

	/**
	 * 
	 * 根据Id删除加密文件(只更改状态,不删除文件) <br>
	 * 同步方法
	 * 
	 * @param id
	 *            文件ID
	 * @return int 操作结果<br>
	 *         1 成功<br>
	 *         -1 删除异常<br>
	 * @throws IllegalArgumentException
	 *             id为0
	 */
	public int deleteEncryptFileByFileId(long fileId) {
		if (fileId == 0) {
			throw new IllegalArgumentException(
					"StorageModule.deleteEncryptFileByFileId(long id) id is 0");
		}

		return FileLocalModule.getInstance().updateEncryptFile(fileId, false);
	}

	/**
	 * 
	 * 根据文件路径删除文件 <br>
	 * 同步方法
	 * 
	 * @param filePath
	 *            文件路径
	 * @return int 操作结果<br>
	 *         1 成功<br>
	 *         -1 删除媒体库记录失败<br>
	 *         -2 删除异常<br>
	 * @throws NullPointerException
	 *             filePath为null
	 * @throws IllegalArgumentException
	 *             filePath为空字符串
	 */
	public int deleteFileByFilePath(String filePath) {
		if (filePath == null) {
			throw new IllegalArgumentException(
					"StorageModule.deleteFileByFilePath(String filePath) filePath is null");
		}

		if ("".equals(filePath)) {
			throw new IllegalArgumentException(
					"StorageModule.deleteFileByFilePath(String filePath) filePath is empty");
		}

		return FileLocalModule.getInstance().deleteFileByFilePath(filePath);
	}

	/**
	 * 
	 * 根据文件ID添加加密文件(只更改状态,不添加文件) <br>
	 * 同步方法
	 * 
	 * @param fileId
	 *            文件ID
	 * @return int 操作结果<br>
	 *         1 插入成功 <br>
	 *         -1 插入异常<br>
	 * @throws IllegalArgumentException
	 *             id为0
	 */
	public int addEncryptFileByFileId(long fileId) {

		if (fileId == 0) {
			throw new IllegalArgumentException(
					"StorageModule.addEncryptFileByFileId(long id) id is 0");
		}

		return FileLocalModule.getInstance().updateEncryptFile(fileId, true);
	}

	/**
	 * 
	 * 获取当前扫描到的视频数量 <br>
	 * 同步方法
	 * 
	 * @return int 视频数量<br>
	 */
	public int getVideoCount() {
		return FileLocalModule.getInstance().getFileCount();
	}

	/**
	 * 
	 * 获取密码
	 * 
	 * @return String 操作结果<br>
	 *         null 或 "" 密码不存在<br>
	 */
	public String getUserPassword() {
		return UserDataBaseAdapter.getInstance().getUserPassword();
	}

	/**
	 * 
	 * 更新密码
	 * 
	 * @param password
	 *            密码
	 * @return int 插入结果 <br>
	 *         1 成功 <br>
	 *         -1 插入异常 <br>
	 *         -2更新异常 <br>
	 * @throws NullPointerException
	 *             password为null
	 * @throws IllegalArgumentException
	 * <br>
	 *             password为空<br>
	 *             password长度不是4位
	 */
	public int updateUserPassword(String password) {
		if (password == null) {
			throw new NullPointerException(
					"StorageModule.updateUserPassword(String password) password is null");
		}
		if ("".equals(password)) {
			throw new IllegalArgumentException(
					"StorageModule.updateUserPassword(String password) password is empty");
		}
		if (password.length() != 4) {
			throw new IllegalArgumentException(
					"StorageModule.updateUserPassword(String password) password length must be 4");
		}

		return UserDataBaseAdapter.getInstance().updatePassword(password);
	}

	/**
	 * 
	 * 添加缓存条目<br>
	 * 
	 * @param offlineCache
	 *            缓存条目
	 * @return int 插入结果<br>
	 *         1 成功 <br>
	 *         -1 插入异常
	 * @throws NullPointerException
	 *             offlineCache为null
	 */
	public int addCache(OfflineCache offlineCache) {
		if (offlineCache == null) {
			throw new NullPointerException(
					"StorageModule.addCache(OfflineCache offlineCache) offlineCache is null");
		}
		List<OfflineCache> offlineCacheList = new ArrayList<OfflineCache>();
		offlineCacheList.add(offlineCache);
		return addCacheList(offlineCacheList);
	}

	/**
	 * 
	 * 添加缓存条目集合<br>
	 * 
	 * @param offlineCacheList
	 *            缓存条目集合
	 * @return int 插入结果<br>
	 *         1 成功 <br>
	 *         -1 插入异常
	 * @throws NullPointerException
	 *             offlineCache为null
	 */
	public int addCacheList(List<OfflineCache> offlineCacheList) {
		if (offlineCacheList == null) {
			throw new NullPointerException(
					"StorageModule.addCacheList(List<OfflineCache> offlineCacheList) offlineCacheList is null");
		}

		// 做添加排序
		Collections.sort(offlineCacheList, new OfflineCacheFileComparator());

		OfflineCacheModule.getInstance().addCacheList(offlineCacheList);

		return 1;
	}

	/**
	 * 开启后台离线缓存服务
	 * 
	 * @return void
	 */
	public void startCacheService() {
		OfflineCacheModule.getInstance().startCacheService();
	}

	/**
	 * 停止后台离线缓存服务
	 * 
	 * @return void
	 */
	public void stopCacheService() {
		OfflineCacheModule.getInstance().stopCacheService();
	}

	/**
	 * 开始一个条目的下载
	 * 
	 * @return void
	 */
	public void startCache(OfflineCache offlineCache) {
		OfflineCacheModule.getInstance().startCache(offlineCache);
	}

	/**
	 * 开始全部下载 MSG_ACTION_OPERATE_PROGRAM_TYPE
	 * 
	 * StorageModule.MSG_ACTION_OPERATE_PROGRAM_TYPE 核心层自动判断,记录上一次下载状态<br>
	 * StorageModule.MSG_ACTION_OPERATE_USER_TYPE 用户主动请求,开始所有
	 * 
	 * @return void
	 */
	public void startAllCache() {
		switch (DownloadUtil.getNetType()) {
		case StorageModule.MSG_WIFI_NETWORK_TYPE: {

			L.v(TAG, "startCacheAllQueue", "MSG_WIFI_NETWORK_TYPE");
			// 开始所有
			OfflineCacheModule.getInstance().startCacheAllQueue();

			break;
		}
		case StorageModule.MSG_NO_NETWORK_TYPE:
			// 无网
			break;
		case StorageModule.MSG_2G_NETWORK_TYPE:
		case StorageModule.MSG_3G_NETWORK_TYPE:
		case StorageModule.MSG_4G_NETWORK_TYPE:
			boolean isAutoDownload = SharedPreferenceModule.getInstance()
					.getBoolean(FoneConstant.AUTO_DOWNLOAD_FLAG_SP);
			L.v(TAG, "startCacheAllQueue",
					"MSG_UNWIFI_NETWORK_TYPE isAutoDownload=" + isAutoDownload);
			if (!isAutoDownload) {
				// 开始所有,仅wifi下载开关关闭(不进行网络类型过滤)
				OfflineCacheModule.getInstance().startCacheAllQueue();
			}
			break;
		default:

			break;
		}

	}

	/**
	 * 暂停一个条目的下载
	 * 
	 * @return void
	 */
	public void pauseCache(OfflineCache offlineCache) {
		OfflineCacheModule.getInstance().pauseCache(offlineCache);
	}

	/**
	 * 暂停所有条目的下载
	 * 
	 * @param isUpdateDownloadState
	 *            全部暂停时,是否保留当前状态<br>
	 *            true保留,false不保留<br>
	 * @return void
	 */
	public void pauseAllCache(boolean isUpdateDownloadState) {
		OfflineCacheModule.getInstance().pauseCacheAllQueue(
				isUpdateDownloadState);
	}

	/**
	 * 删除指定条目列表的下载
	 * 
	 * @return void
	 */
	public void deleteCacheList(List<OfflineCache> offlineCacheList) {
		OfflineCacheModule.getInstance().deleteCacheList(offlineCacheList);
	}

	/**
	 * 
	 * 获取缓存文件列表的数量
	 * 
	 * @return int 缓存文件数量
	 */
	public int getOfflineCacheFileCount() {
		return OfflineCacheFileDataBaseAdapter.getInstance()
				.getOfflineCacheFileFinishCount();
	}

	/**
	 * 获取缓存第一级目录集合
	 * 
	 * @return void
	 */
	public void getCacheFolderList() {
		OfflineCacheModule.getInstance().getOfflineCacheFolderList();
	}

	/**
	 * 
	 * 根据cid获取缓存文件信息
	 * 
	 * @return OfflineCache 缓存文件信息
	 */
	public OfflineCache getCacheFile(long cid) {
		return OfflineCacheModule.getInstance().mergeOfflineCacheFile(cid);
	}

	/**
	 * 
	 * 合并非剧集类型已下载完成缓存文件的分片连接
	 * 
	 * @return ArrayList<OfflineCache> 非剧集类型中的视频
	 */
	public ArrayList<OfflineCache> getCacheFileListStateFinishUnDrama() {
		return OfflineCacheModule.getInstance()
				.mergeCacheFileListStateFinishUnDrama();
	}

	/**
	 * 
	 * 合并剧集类型已下载完成缓存文件的分片连接
	 * 
	 * @param offlineCache
	 *            剧集文件夹
	 * 
	 * @return ArrayList<OfflineCache> 剧集类型中的所有视频
	 */
	public ArrayList<OfflineCache> getCacheFileListStateFinishDramaByOfflineCache(
			OfflineCache offlineCache) {
		return OfflineCacheModule.getInstance()
				.mergeCacheFileListStateFinishDrama(offlineCache);
	}

	/**
	 * 
	 * 合并已下载15%缓存文件的分片连接
	 * 
	 * @param offlineCache
	 *            缓存文件
	 * @return void
	 */
	public void mergeCacheFilePlayableByCID(OfflineCache offlineCache) {
		OfflineCacheModule.getInstance().mergeCacheFilePlayableByCID(
				offlineCache);
	}

	/**
	 * 
	 * 根据CID合并更多剧集下载状态
	 * 
	 * @param offlineCacheList
	 *            待合并集合
	 * @return List<OfflineCache>
	 */
	public List<OfflineCache> mergeMoreDramaDownloadStateByCID(
			List<OfflineCache> offlineCacheList) {
		return OfflineCacheModule.getInstance()
				.mergeMoreDramaDownloadStateByCID(offlineCacheList);
	}

	/**
	 * 
	 * 根据VideoId合并收藏下载状态
	 * 
	 * @param offlineCacheList
	 *            待合并集合
	 * @return List<OfflineCache>
	 */
	public List<OfflineCache> mergeFavouriteDownloadStateByVideoId(
			List<OfflineCache> offlineCacheList) {
		return FavouriteModule.getInstance()
				.mergeFavouriteDownloadStateByVideoId(offlineCacheList);

	}

	/**
	 * 
	 * 根据缓存CID获取缓存对象
	 * 
	 * @param CID
	 *            缓存CID
	 * @return boolean 缓存对象
	 */
	public boolean isOfflineCacheFileByCID(long CID) {
		return OfflineCacheFileDataBaseAdapter.getInstance()
				.isOfflineCacheFileByCID(CID);
	}

	/**
	 * 
	 * 查询所有出错的cid集合
	 * 
	 * @return ArrayList<Long> 缓存CID集合
	 */
	public ArrayList<Long> getCIDListByError() {
		return OfflineCacheFileDataBaseAdapter.getInstance()
				.getCIDListByError();
	}

	/**
	 * 
	 * 根据缓存ID获取缓存集合 <br>
	 * 同步方法
	 * 
	 * @param CCID
	 *            缓存条目Id
	 * @return ArrayList<OfflineCache> 缓存集合
	 * @throws IllegalArgumentException
	 *             cacheId是0
	 */
	public void getCacheFileListByCCID(long CCID) {
		if (CCID == 0) {
			throw new IllegalArgumentException(
					"StorageModule.getCacheFileListByCID(long CCID) CCID is 0");
		}
		OfflineCacheModule.getInstance().getOfflineCacheFileList(CCID);
	}

	/**
	 * 
	 * 根据缓存CID获取缓存对象
	 * 
	 * @return OfflineCache 缓存对象
	 */
	public OfflineCache getOfflineCacheFileByCID(long CID) {
		return OfflineCacheFileDataBaseAdapter.getInstance()
				.getOfflineCacheFileByCID(CID);
	}

	/**
	 * 
	 * 添加收藏条目(单个)<br>
	 * 
	 * @param offlineCache
	 *            收藏条目
	 * @return int 插入结果<br>
	 *         1 成功 <br>
	 *         -1 插入异常
	 * @throws NullPointerException
	 *             offlineCache为null
	 */
	public int addFavourite(OfflineCache offlineCache) {
		if (offlineCache == null) {
			throw new NullPointerException(
					"StorageModule.addOfflineCache(OfflineCache offlineCache) offlineCache is null");
		}
		List<OfflineCache> offlineCacheList = new ArrayList<OfflineCache>();
		offlineCacheList.add(offlineCache);
		return addFavouriteList(offlineCacheList);
	}

	/**
	 * 
	 * 添加收藏条目集合(批量)<br>
	 * 
	 * @param offlineCacheList
	 *            收藏条目
	 * @return int 插入结果<br>
	 *         1 成功 <br>
	 *         -1 插入异常
	 * @throws NullPointerException
	 *             offlineCacheList为null
	 */
	public int addFavouriteList(List<OfflineCache> offlineCacheList) {
		if (offlineCacheList == null) {
			throw new NullPointerException(
					"StorageModule.addFavouriteList(List<OfflineCache> offlineCacheList) offlineCacheList is null");
		}
		return FavouriteDataBaseAdapter.getInstance().mergeFavouriteList(
				offlineCacheList);
	}

	/**
	 * 
	 * 获得第一级收藏集合 <br>
	 * 同步方法
	 * 
	 * @return ArrayList<OfflineCache> 缓存集合 <br>
	 */
	public ArrayList<OfflineCache> getFavouriteList() {

		return FavouriteModule.getInstance().getFavouriteList();
	}

	/**
	 * 
	 * 判断收藏是否存在
	 * 
	 * @return boolean 是否收藏<br>
	 *         true 已收藏<br>
	 *         false 未收藏
	 */
	public boolean isFavourite(long videoId) {
		return FavouriteDataBaseAdapter.getInstance().isFavourite(videoId);
	}

	/**
	 * 
	 * 获得收藏集合 <br>
	 * 同步方法
	 * 
	 * @param synchronzieType
	 *            同步类型<br>
	 *            OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_ADD_ALREADY 已同步<br>
	 *            OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_ADD_WAIT 待添加<br>
	 *            OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_DELETE_WAIT 待删除<br>
	 *            OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_ALL 全部收藏<br>
	 *            OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_ALL_WAIT 全部待收藏<br>
	 * @return ArrayList<OfflineCache> 缓存集合
	 */
	public ArrayList<OfflineCache> getFavouriteList(int synchronzieType) {
		return FavouriteDataBaseAdapter.getInstance().getFavouriteList(
				synchronzieType);
	}

	/**
	 * 
	 * 获得收藏集合数量 <br>
	 * 同步方法
	 * 
	 * @param synchronzieType
	 *            同步类型<br>
	 *            OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_ADD_ALREADY 已同步<br>
	 *            OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_ADD_WAIT 待添加<br>
	 *            OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_DELETE_WAIT 待删除<br>
	 *            OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_ALL 全部收藏<br>
	 *            OfflineCache.CACHE_FAVOURITE_SYNCHRONZIE_ALL_WAIT 全部待收藏<br>
	 * @return int 缓存集合数量
	 */
	public int getFavouriteCount(int synchronzieType) {
		return FavouriteDataBaseAdapter.getInstance().getFavouriteCount(
				synchronzieType);
	}

	/**
	 * 
	 * 删除收藏 (单个)<br>
	 * 同步方法
	 * 
	 * @param offlineCache
	 *            收藏对象
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 * @throws IllegalArgumentException
	 *             CID是0
	 */
	public int deleteFavourite(OfflineCache offlineCache) {
		List<OfflineCache> offlineCacheList = new ArrayList<OfflineCache>();
		offlineCacheList.add(offlineCache);
		return FavouriteDataBaseAdapter.getInstance().deleteFavouriteList(
				offlineCacheList);
	}

	/**
	 * 
	 * 删除所有收藏 <br>
	 * 同步方法
	 * 
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 */
	public int deleteFavouriteAll() {
		return FavouriteDataBaseAdapter.getInstance().deleteFavouriteAll();
	}

	/**
	 * 
	 * 删除收藏(批量) <br>
	 * 同步方法
	 * 
	 * @param offlineCacheList
	 *            收藏对象
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 */
	public int deleteFavourite(List<OfflineCache> offlineCacheList) {
		return FavouriteDataBaseAdapter.getInstance().deleteFavouriteList(
				offlineCacheList);
	}

	/**
	 * 更新下载状态
	 * 
	 * @param offlineCache
	 *            缓存对象
	 * @return int 操作结果<br>
	 *         1 更新成功<br>
	 *         -1 更新异常<br>
	 */
	public int updateSelectedDownloadState(OfflineCache offlineCache) {
		return FavouriteDataBaseAdapter.getInstance()
				.updateSelectedDownloadState(offlineCache);
	}

	/**
	 * 
	 * 更新同步状态(批量更新) <br>
	 * 同步方法
	 * 
	 * @param offlineCacheList
	 *            待更新状态集合
	 * @return int 操作结果<br>
	 *         1 更新成功<br>
	 *         -1 更新异常<br>
	 */
	public int updateFavouriteSynchronzieType(
			List<OfflineCache> offlineCacheList) {
		return FavouriteDataBaseAdapter.getInstance()
				.updateFavouriteSynchronzieTypeByOfflineCacheList(
						offlineCacheList);
	}

	/**
	 * 
	 * 更新同步状态(单个更新) <br>
	 * 同步方法
	 * 
	 * @param offlineCache
	 *            待更新状态集合
	 * @return int 操作结果<br>
	 *         1 更新成功<br>
	 *         -1 更新异常<br>
	 */
	public int updateFavouriteSynchronzieType(OfflineCache offlineCache) {
		List<OfflineCache> offlineCacheList = new ArrayList<OfflineCache>();
		offlineCacheList.add(offlineCache);
		return FavouriteDataBaseAdapter.getInstance()
				.updateFavouriteSynchronzieTypeByOfflineCacheList(
						offlineCacheList);
	}

	/**
	 * 
	 * 添加播放记录
	 * 
	 * @param playRecord
	 *            播放记录对象
	 * @return int 插入结果<br>
	 *         1 插入成功 <br>
	 *         -1 插入异常
	 * @throws NullPointerException
	 *             playRecord为null
	 */
	public int addPlayRecord(PlayRecord playRecord) {
		if (playRecord == null) {
			throw new NullPointerException(
					"StorageModule.addPlayRecord(PlayRecord playRecord) playRecord is null");
		}

		return PlayRecordDataBaseAdapter.getInstance()
				.addPlayRecord(playRecord);
	}

	/**
	 * 
	 * 添加播放记录
	 * 
	 * @param playRecord
	 *            播放记录对象
	 * @return int 插入结果<br>
	 *         1 插入成功 <br>
	 *         -1 插入异常
	 * @throws NullPointerException
	 *             playRecord为null
	 */
	public int addPlayRecordList(List<PlayRecord> playRecordList) {
		if (playRecordList == null) {
			throw new NullPointerException(
					"StorageModule.addPlayRecord(List<PlayRecord> playRecordList) playRecordList is null");
		}
		return PlayRecordDataBaseAdapter.getInstance().addPlayRecordList(
				playRecordList);
	}

	public int updatePlayRecord(PlayRecord playRecord) {
		if (playRecord == null) {
			throw new NullPointerException(
					"StorageModule.updatePlayRecord(PlayRecord playRecord) playRecord is null");
		}
		List<PlayRecord> playRecordList = new ArrayList<PlayRecord>();
		playRecordList.add(playRecord);
		return updatePlayRecordList(playRecordList);
	}

	public int updatePlayRecordList(List<PlayRecord> playRecordList) {
		if (playRecordList == null) {
			throw new NullPointerException(
					"StorageModule.updatePlayRecordList(List<PlayRecord> playRecordList) playRecordList is null");
		}
		return PlayRecordDataBaseAdapter.getInstance().updatePlayRecordList(
				playRecordList);
	}

	/**
	 * 
	 * 根据保留记录的数量删除多余的记录 <br>
	 * 只支持本地记录和在线记录 同步方法
	 * 
	 * @param persistNum
	 *            保留数目
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 * @throws IllegalArgumentException
	 *             persistNum是0
	 */
	public int deletePlayRecordByPersistNum(int persistNum) {
		if (persistNum == 0) {
			throw new IllegalArgumentException(
					"StorageModule.deletePlayRecordByPersistNum(int persistNum) persistNum is 0");
		}
		return PlayRecordDataBaseAdapter.getInstance()
				.deletePlayRecordByPersistNum(persistNum);
	}

	public int deletePlayRecord(PlayRecord playRecord) {
		return PlayRecordDataBaseAdapter.getInstance().deletePlayRecord(
				playRecord);
	}

	/**
	 * 
	 * 根据类型删除播放记录
	 * 
	 * @param playRecordType
	 *            播放记录类型
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 */
	public int deletePlayRecordList(int playRecordType) {
		return PlayRecordDataBaseAdapter.getInstance().deletePlayRecord(
				playRecordType);

	}

	/**
	 * 
	 * 根据同步类型获得播放记录集合中的第一个记录(根据时间倒序)
	 * 
	 * @return PlayRecord 播放记录对象
	 */
	public PlayRecord getPlayRecordByPlayRecordSynchronzieType() {
		return PlayRecordDataBaseAdapter.getInstance()
				.getPlayRecordByPlayRecordSynchronzieType();
	}

	/**
	 * 
	 * 获得播放记录集合 <br>
	 * 同步方法
	 * 
	 * @return ArrayList<PlayRecord> 播放记录集合
	 */
	public ArrayList<PlayRecord> getPlayRecordList(int playeRecordType) {
		return PlayRecordDataBaseAdapter.getInstance().getPlayRecordList(
				playeRecordType);
	}

	/**
	 * 
	 * 根据播放地址获得播放记录
	 * 
	 * @param playUrl
	 *            播放地址
	 * @return PlayRecord 播放记录
	 * @throws NullPointerException
	 *             playUrl为null
	 * @throws IllegalArgumentException
	 *             playUrl为空<br>
	 */
	public PlayRecord getPlayRecordByPlayUrl(String playUrl) {
		if (playUrl == null) {
			throw new NullPointerException(
					"StorageModule.getPlayRecordByPlayUrl(String playUrl) playUrl is null");
		}
		if ("".equals(playUrl)) {
			throw new IllegalArgumentException(
					"StorageModule.getPlayRecordByPlayUrl(String playUrl) playUrl is empty");
		}
		return PlayRecordDataBaseAdapter.getInstance().getPlayRecordByPlayUrl(
				playUrl);
	}

	/**
	 * 
	 * 根据VideoID获得播放记录
	 * 
	 * @param videoID
	 *            片库ID
	 * @return PlayRecord 播放记录
	 */
	public PlayRecord getPlayRecordByVideoID(long videoID) {
		return PlayRecordDataBaseAdapter.getInstance().getPlayRecordByVideoID(
				videoID);
	}

	/**
	 * 
	 * 根据CID获得播放记录
	 * 
	 * @param CID
	 *            内容ID
	 * @return PlayRecord 播放记录
	 * @throws IllegalArgumentException
	 *             CID为0<br>
	 */
	public PlayRecord getPlayRecordByCID(long CID) {
		if (CID == 0) {
			throw new IllegalArgumentException(
					"StorageModule.getPlayRecordByCID(long CID) CID is 0");
		}
		return PlayRecordDataBaseAdapter.getInstance().getPlayRecordByCID(CID);
	}

	/**
	 * 
	 * 根据VideoID获得最近的播放记录
	 * 
	 * @param VideoID
	 *            片库ID
	 * @param CCID
	 *            内容集合ID
	 * @return PlayRecord 播放记录
	 */
	public PlayRecord getRecentlyPlayRecordByID(long videoID, long CCID) {
		return PlayRecordDataBaseAdapter.getInstance()
				.getRecentlyPlayRecordByID(videoID, CCID);
	}

	/**
	 * 
	 * 添加单个通知 <br>
	 * 同步方法
	 * 
	 * @param notification
	 *            通知对象
	 * @return int 插入结果<br>
	 *         1 插入成功 <br>
	 *         -1 插入异常
	 * @throws NullPointerException
	 *             notification为null
	 */
	public int addNotification(NotificationBean notification) {
		if (notification == null) {
			throw new NullPointerException(
					"StorageModule.addNotification(Notification notification) notification is null");
		}
		List<NotificationBean> notificationBeanList = new ArrayList<NotificationBean>();
		notificationBeanList.add(notification);
		return NotificationDataBaseAdapter.getInstance().addNotificationList(
				notificationBeanList);
	}

	/**
	 * 
	 * 添加多个通知 <br>
	 * 同步方法
	 * 
	 * @param notificationList
	 *            通知对象集合
	 * @return int 插入结果<br>
	 *         1 插入成功 <br>
	 *         -1 插入异常
	 * @throws NullPointerException
	 *             notification为null
	 */
	public int addNotificationList(List<NotificationBean> notificationList) {
		if (notificationList == null) {
			throw new NullPointerException(
					"StorageModule.addNotification(List<NotificationBean> notificationList) notificationList is null");
		}
		return NotificationDataBaseAdapter.getInstance().addNotificationList(
				notificationList);
	}

	/**
	 * 
	 * 根据展示状态获取所有消息
	 * 
	 * @param isShow
	 *            是否展示
	 * @return ArrayList<NotificationBean> 通知集合
	 */
	public ArrayList<NotificationBean> getNotificationListByIsShow(
			boolean isShow) {
		return NotificationDataBaseAdapter.getInstance()
				.getNotificationListByIsShow(isShow);
	}

	/**
	 * 
	 * 按时间排序,根据消息类型分页查询所有消息 <br>
	 * 同步方法
	 * 
	 * @param notificationType
	 *            通知列表类型<br>
	 *            {@link com.fone.player.entity.NotificationBean.NOTIFICATION_TYPE
	 *            Notification.NOTIFICATION_TYPE} 通知类型<br>
	 *            {@link com.fone.player.entity.NotificationBean.INFORMATION_TYPE
	 *            Notification.INFORMATION_TYPE} 消息类型<br>
	 * @param pageNum
	 *            页码
	 * @param pageSize
	 *            页容量
	 * @return ArrayList<NotificationBean> 通知集合
	 * @throws IllegalArgumentException
	 *             notificationType取值范围有误
	 */
	public ArrayList<NotificationBean> getNotificationList(boolean isNew,
			int notificationType, int notificationId, int pageSize) {
		return NotificationDataBaseAdapter.getInstance().getNotificationList(
				isNew, notificationType, notificationId, pageSize);
	}

	/**
	 * 
	 * 根据服务端通知ID查询通知对象
	 * 
	 * @param notificationServerId
	 *            服务器段通知ID
	 * @return NotificationBean 通知对象
	 */
	public NotificationBean getNotificationByServerId(
			String notificationServerId) {
		return NotificationDataBaseAdapter.getInstance()
				.getNotificationByServerId(notificationServerId);
	}

	/**
	 * 根据通知对象更新消息读取状态
	 * 
	 * @param notificationBean
	 *            通知对象
	 * @return int 操作结果<br>
	 *         1 更新成功<br>
	 *         -1 更新异常<br>
	 */
	public int updateNotificationIsRead(NotificationBean notificationBean) {
		return NotificationDataBaseAdapter.getInstance()
				.updateNotificationIsRead(notificationBean);
	}

	/**
	 * 更新消息展示状态
	 * 
	 * @param notificationBeanList
	 *            通知对象集合
	 * @return int 操作结果<br>
	 *         1 更新成功<br>
	 *         -1 更新异常<br>
	 */
	public int updateNotificationIsShow(
			List<NotificationBean> notificationBeanList) {
		return NotificationDataBaseAdapter.getInstance()
				.updateNotificationIsShow(notificationBeanList);
	}

	/**
	 * 
	 * 根据消息标题删除单个消息 <br>
	 * 同步方法
	 * 
	 * @param notificationBean
	 *            消息对象
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 */
	public int deleteNotificationById(NotificationBean notificationBean) {
		List<NotificationBean> notificationBeanList = new ArrayList<NotificationBean>();
		notificationBeanList.add(notificationBean);
		return NotificationDataBaseAdapter.getInstance()
				.deleteNotificationById(notificationBeanList);
	}

	/**
	 * 
	 * 根据消息标题删除多个消息 <br>
	 * 同步方法
	 * 
	 * @param notificationBeanList
	 *            消息集合
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 */
	public int deleteNotificationById(
			List<NotificationBean> notificationBeanList) {
		return NotificationDataBaseAdapter.getInstance()
				.deleteNotificationById(notificationBeanList);
	}

	/**
	 * 
	 * 根据文件URI返回文件路径
	 * 
	 * @param uri
	 *            文件URI
	 * @return String 文件路径
	 * @throws NullPointerException
	 *             uri为null
	 * @throws IllegalArgumentException
	 *             uri为空<br>
	 */
	public String getFilePathByMediaUri(String uri) {
		if (uri == null) {
			throw new NullPointerException(
					"StorageModule.getFilePathByMediaUri(String uri) uri is null");
		}
		if ("".equals(uri)) {
			throw new IllegalArgumentException(
					"StorageModule.getFilePathByMediaUri(String uri) uri is empty");
		}
		return FileLocalModule.getInstance().getFilePathByMediaUri(uri);
	}

	/**
	 * 
	 * 取消扫描<br>
	 * 注意:该方法只取消scanningAllStorage()和scanningMediaStore()的操作.
	 * 
	 * @return 状态码 (msg.what):<br>
	 *         {@link com.fone.player.entity.MediaFolder.MSG_ACTION_SCANNER_STOPPED
	 *         StorageModule.MSG_ACTION_SCANNER_STOPPED} 扫描全盘文件停止<br>
	 *         Example:<br>
	 * 
	 *         <pre>
	 * public void onEventMainThread(Message msg) {
	 * 	switch (msg.what) {
	 * 	case StorageModule.MSG_ACTION_SCANNER_STOPPED:
	 * 		break;
	 * 	}
	 * }
	 * </pre>
	 */
	public void cancelScanning() {
		FileLocalModule.getInstance().cancelScanning();
	}

	/**
	 * 
	 * 添加预约节目
	 * 
	 * @param contentSubscribe
	 *            预约节目
	 * @return int 插入结果<br>
	 *         1 插入成功 <br>
	 *         -1 插入异常
	 * 
	 */
	public int addContentSubscribe(ContentSubscribe contentSubscribe) {
		return ContentSubscribeDataBaseAdapter.getInstance()
				.addContentSubscribe(contentSubscribe);
	}

	/**
	 * 
	 * 根据ID获得预约节目
	 * 
	 * @param contentSubscribeId
	 *            预约节目id
	 * @return ContentSubscribe 预约节目
	 */
	public ContentSubscribe getContentSubscribeById(int contentSubscribeId) {
		return ContentSubscribeDataBaseAdapter.getInstance()
				.getContentSubscribeById(contentSubscribeId);
	}

	/**
	 * 
	 * 获取距离当前时间最近的节目单
	 * 
	 * @return ContentSubscribe 预约节目
	 */
	public ContentSubscribe getNearestContentSubscribe() {
		return ContentSubscribeDataBaseAdapter.getInstance()
				.getNearestContentSubscribe();
	}

	/**
	 * 
	 * 根据时间和名字获得预约节目
	 * 
	 * @param contentSubscribeName
	 *            预约节目名字
	 * @param contentSubscribeTime
	 *            预约节目时间
	 * @return ContentSubscribe 预约节目
	 */
	public ContentSubscribe getContentSubscribeByNameAndTime(
			String contentSubscribeName, long contentSubscribeTime) {
		return ContentSubscribeDataBaseAdapter.getInstance()
				.getContentSubscribeByNameAndTime(contentSubscribeName,
						contentSubscribeTime);
	}

	/**
	 * 
	 * 获得所有预约节目列表
	 * 
	 * @return List<ContentSubscribe> 预约节目集合
	 */
	public List<ContentSubscribe> getContentSubscribeList() {
		return ContentSubscribeDataBaseAdapter.getInstance()
				.getContentSubscribeList();
	}

	/**
	 * 
	 * 根据ID删除预约节目 <br>
	 * 同步方法
	 * 
	 * @param contentSubscribeId
	 *            预约节目ID
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 */
	public int deleteContentSubscribeById(int contentSubscribeId) {
		return ContentSubscribeDataBaseAdapter.getInstance()
				.deleteContentSubscribeById(contentSubscribeId);
	}

	/**
	 * 
	 * 根据时间删除小于参数时间的预约节目 <br>
	 * 同步方法
	 * 
	 * @param contentSubscribeTime
	 *            预约节目时间
	 * @return int 操作结果<br>
	 *         1 删除成功<br>
	 *         -1 删除异常<br>
	 */
	public int deleteContentSubscribeByTime(long contentSubscribeTime) {
		return ContentSubscribeDataBaseAdapter.getInstance()
				.deleteContentSubscribeByTime(contentSubscribeTime);
	}

	/**
	 * 
	 * 添加自定义频道
	 * 
	 * @param customChannelList
	 *            自定义频道集合
	 * @return int 操作结果<br>
	 *         1插入成功<br>
	 *         -1 插入异常
	 * 
	 */
	public int addCustomChannelList(List<CustomChannel> customChannelList) {
		return CustomChannelDataBaseAdapter.getInstance().addCustomChannelList(
				customChannelList);
	}

	/**
	 * 
	 * 获得所有自定义频道列表
	 * 
	 * @return List<CustomChannel> 自定义频道集合
	 */
	public List<CustomChannel> getCustomChannelList() {
		return CustomChannelDataBaseAdapter.getInstance()
				.getCustomChannelList();
	}

	/**
	 * 
	 * 添加下载任务(目前仅支持apk下载)
	 * 
	 * @param download
	 *            下载对象
	 * @return int 操作结果<br>
	 *         1 添加成功<br>
	 *         0 服务未启动<br>
	 *         -1 任务已存在 <br>
	 *         -2添加异常
	 */
	public int addFileDownload(Download download) {
		return CacheDownloadManager.getInstance().addDownloadFile(download);
	}

	/**
	 * 
	 * 更新下载任务(目前仅支持apk下载)
	 * 
	 * @param download
	 *            下载对象
	 * @return int 操作结果<br>
	 *         1 更新成功<br>
	 *         -1 任务不存在 <br>
	 *         -2 任务已停止 <br>
	 */
	public int updateFileDownload(Download download) {
		return CacheDownloadManager.getInstance().updateDownloadFile(download);
	}

	/**
	 * 
	 * 根据URL获取下载对象
	 * 
	 * @param url
	 *            下载url
	 * @return download 下载对象
	 */
	public Download getDownload(String url) {
		return FileDownloadDataBaseAdapter.getInstance().getDownload(url);
	}

	/**
	 * 安装下载后的apk文件
	 * 
	 * @param filePath
	 *            文件本地路径
	 * @return void
	 */
	public void installApp(String filePath, String downloadUrl) {

		NotificationHandle.getInstance().installApp(filePath);
	}

	/**
	 * 
	 * 开始发送数据
	 * 
	 * @return void
	 */
	public void startSendOfflineCacheMessage() {
		OfflineCacheModule.getInstance().startSendOfflineCacheMessage();
	}

	/**
	 * 
	 * 停止发送数据
	 * 
	 * @return void
	 */
	public void stopSendOfflineCacheMessage() {
		OfflineCacheModule.getInstance().stopSendOfflineCacheMessage();
	}

	/**
	 * 
	 * 检查存储空间(单次检测)
	 * 
	 * @return boolean
	 */
	public void checkSingleStorageSpace() {
		new Thread(new Runnable() {

			@Override
			public void run() {

				CacheDownloadManager.getInstance().checkSingleStorageSpace();

			}
		}).start();
	}

	/**
	 * 打印栈信息
	 * 
	 * @return void
	 */
	public void writeStackTrace() {
		StackTraceElement[] stackElements = new Throwable().getStackTrace();
		if (stackElements != null) {
			for (int i = 0; i < stackElements.length; i++) {
				L.v("", "" + stackElements[i]);
			}
		}
	}

	/**
	 * 文件拷贝
	 * 
	 * @param sourceFile
	 * @param targetFile
	 * @throws IOException
	 */
	public void copyDataBaseToSdCard() {
		BufferedInputStream inBuff = null;
		BufferedOutputStream outBuff = null;
		try {
			FileInputStream source = new FileInputStream(
					sContext.getDatabasePath("fonePlayer.db"));
			String folderPath = Environment.getExternalStorageDirectory()
					.getAbsolutePath() + File.separator + "100tv";
			File folder = new File(folderPath);
			if (!folder.exists()) {
				folder.mkdirs();
			}

			File filepath = new File(folderPath + "/foneplayer.db");
			FileOutputStream target = new FileOutputStream(filepath);

			// 新建文件输入流并对它进行缓冲
			inBuff = new BufferedInputStream(source);
			// 新建文件输出流并对它进行缓冲
			outBuff = new BufferedOutputStream(target);
			// 缓冲数组
			byte[] b = new byte[1024 * 5];
			int len;
			while ((len = inBuff.read(b)) != -1) {
				outBuff.write(b, 0, len);
			}
			// 刷新此缓冲的输出流
			outBuff.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();

		} finally {
			// 关闭流
			if (inBuff != null)
				try {
					inBuff.close();
				} catch (IOException e) {
					e.printStackTrace();

				}
			if (outBuff != null)
				try {
					outBuff.close();
				} catch (IOException e) {
					e.printStackTrace();

				}
		}
	}

	/**
	 * 
	 * 写log到存储设备
	 * 
	 * @param tag
	 *            标志
	 * @param message
	 *            信息
	 * @return void
	 */
	public void writeLogStorage(String level, String tag, String type,
			String message) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS",
				Locale.getDefault());
		String time = sdf.format(new java.util.Date());
		String content = time + "  " + level + "   " + tag + "   " + type
				+ "   " + message + "\r\n";
		File folder = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "100tv");
		if (!folder.exists()) {
			folder.mkdirs();

		}
		File file = new File(folder.getAbsolutePath() + File.separator
				+ "download_log.txt");
		try {
			if (!file.exists()) {
				file.createNewFile();
			}

		} catch (IOException e) {
			e.printStackTrace();

		}

		OutputStream out = null;
		try {
			out = new FileOutputStream(file);
			out.write(content.getBytes());
		} catch (FileNotFoundException e) {
			if (e != null) {
				e.printStackTrace();
			}

		} catch (IOException e) {
			if (e != null) {
				e.printStackTrace();
			}

		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					if (e != null) {
						e.printStackTrace();
					}

				}
			}
		}

	}
}
